package net.dopan.pigframe.demo.service.impl;

import net.dopan.pigframe.config.utils.ObjToMap;
import net.dopan.pigframe.config.utils.Sort;
import net.dopan.pigframe.core.base.service.impl.BaseServiceImpl;
import net.dopan.pigframe.demo.mapper.UserConcernUserMapper;
import net.dopan.pigframe.demo.model.entity.UserConcernUser;
import net.dopan.pigframe.demo.model.entity.vo.*;
import net.dopan.pigframe.demo.service.UserConcernUserService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional(rollbackFor = RuntimeException.class)
public class UserConcernUserServiceImpl extends BaseServiceImpl<UserConcernUserMapper, UserConcernUser> implements UserConcernUserService {

    @Autowired
    private UserConcernUserMapper userConcernUserMapper;

    @Override
    public UserVo findUserVoByUserId(String userId) {
        return userConcernUserMapper.findUserVoByUserId(userId);
    }

    @Override
    public List<UserNoteVo> findUserNoteVoByUserId(String userId, int offset, int limit) {
        return userConcernUserMapper.findUserNoteVoByUserId(userId, offset, limit);
    }

    @Override
    public List<Map<String, String>> findUserCollectMap(String userId) {
        return userConcernUserMapper.findUserCollectMap(userId);
    }

    @Override
    public void concernUser(String userId, String concernUserId) {

        String concernUsername = userConcernUserMapper.findUsernameById(concernUserId);
        String fansname = userConcernUserMapper.findUsernameById(userId);
        /** 添加关注 */
        userConcernUserMapper.concernUser(userId, concernUserId, concernUsername);
        /**修改用户关注数量 */
        userConcernUserMapper.updateUserConcernNum(userConcernUserMapper.concernCount(userId), userId);
        /** 添加粉丝 */
        userConcernUserMapper.addfans(concernUserId, userId, fansname);
        /** 修改用户粉丝数量 */
        userConcernUserMapper.updatefansNum(userConcernUserMapper.fansCount(concernUserId), concernUserId);
    }

    @Override
    public Integer isConcern(String userId, String concernUserId) {
        /**关注*/
        return userConcernUserMapper.isConcern(userId, concernUserId);
    }

    @Override
    public Integer fansCount(String userId) {
        return userConcernUserMapper.fansCount(userId);
    }

    @Override
    public void cancleConcernUser(String userId, String concernUserId) {

        /** 取消关注 */
        userConcernUserMapper.cancleConcernUser(userId, concernUserId);
        /**修改用户关注数量 */
        userConcernUserMapper.updateUserConcernNum(userConcernUserMapper.concernCount(userId), userId);
        /** 移除粉丝 */
        userConcernUserMapper.reduceFans(concernUserId, userId);
        /** 修改用户粉丝数量 */
        userConcernUserMapper.updatefansNum(userConcernUserMapper.fansCount(concernUserId), concernUserId);
    }

    @Override
    public Map<String, List> getConcernUsers(String userId) {
        List<Map<String, String>> list = userConcernUserMapper.getConcernUsers(userId);
        return Sort.sort(list);
    }

    @Override
    public Map<String, List> getUserFans(String userId) {
        List<Map<String, String>> list = userConcernUserMapper.getUserFans(userId);
        return Sort.sortFans(list);
    }

    @Override
    public Map<String, List<Map<String, String>>> getUserActivityByUserId(String userId) {
        Map<String, List<Map<String, String>>> map = new HashMap<>();
        map.put("参加的活动", userConcernUserMapper.getUserActivityByUserId(userId));
        map.put("已结束", userConcernUserMapper.getFinishUserActivityByUserId(userId));
        return map;
    }

    @Override
    public Integer cancleUserActivity(String userId, String activityId) {
        return userConcernUserMapper.cancleUserActivity(userId, activityId);
    }

    @Override
    public Map<String, List<Map<String, String>>> getLaunchActivityAndEnd(String userId) {
        Map<String, List<Map<String, String>>> map = new HashMap<>();
        map.put("发起的活动", userConcernUserMapper.getLaunchActivity(userId));
        map.put("已结束", userConcernUserMapper.getEndActivity(userId));
        return map;
    }

    @Override
    public Integer delUserActivityByUserId(String userId, String activityId) {

        return userConcernUserMapper.delUserActivityByUser(userId, activityId);
    }

    @Override
    public UserActivityVo1 editUserActivityVo(String userId, String activityId) {
        return userConcernUserMapper.findUserActivityVo1(activityId);
    }

    @Override
    public Map<String, List<Map<String, String>>> getLikeUserActivity(String userId) {
        Map<String, List<Map<String, String>>> map = new HashMap<>();
        map.put("感兴趣的活动", userConcernUserMapper.getLikeUserActivity(userId));
        map.put("已结束", userConcernUserMapper.getFinishLikeUserActivity(userId));
        return map;
    }

    @Override
    public Integer delLikeUserActivity(String userId, String activityId) {
        return userConcernUserMapper.delLikeUserActivity(userId, activityId);
    }

    @Override
    public Integer saveUserFeedBack(UserFeedbackVo userFeedbackVo) {
        return userConcernUserMapper.saveUserFeedBack(userFeedbackVo);
    }

    @Override
    public Integer changUserUUID(String userId, String uuid) {
        return userConcernUserMapper.changUserUUID(userId, uuid);
    }

    @Override
    public Integer findUserUUIDFlag(String userId) {
        return userConcernUserMapper.findUserUUIDFlag(userId);
    }

    @Override
    public List<Map<String, String>> getCancleReason() {
        return userConcernUserMapper.getCancleReason();
    }

    @Override
    public Integer saveUserActivityCancle(UserActivityCancleVo userActivityCancleVo) {
        return userConcernUserMapper.saveUserActivityCancle(userActivityCancleVo);
    }

    @Override
    public Map<String, List<Map<String, String>>> activityCalendar(String userId, Date date) {

        Map<String, List<Map<String, String>>> map = new HashMap<>();
        /** 用户发起的活动 */
        List<Map<String, String>> list = userConcernUserMapper.getLaunchActivityByDate(userId, date);
        map.put("发起的活动", list);
        /** 用户感兴趣的活动*/
        List<Map<String, String>> list1 = userConcernUserMapper.getLikeUserActivityByDate(userId, date);
        map.put("感兴趣的活动", list1);
        /** 用户参加的活动*/
        List<Map<String, String>> list2 = userConcernUserMapper.getUserActivityByDate(userId, date);
        map.put("参加的活动", list2);
        /** 用户收藏的活动 */
        List<Map<String, String>> list3 = userConcernUserMapper.getUserActivityCollectByDate(userId, date);
        map.put("收藏的活动", list3);
        return map;
    }

    @Override
    public List<Map<String, String>> getUserEvent(String userId, Integer offset, Integer limit) {

        List<Map<String, String>> lists = userConcernUserMapper.getUserEvent(userId, offset, limit);
        Map havePageMap = new HashMap();
        if (lists.size() < limit) {
            havePageMap.put("havePage", "false");
        } else {
            havePageMap.put("havePage", "true");
        }
        lists.add(havePageMap);
        for (int i = 0; i < lists.size(); i++) {
            /** 发布活动 */
            Map<String, String> map2 = lists.get(i);
            String event_type_id = map2.get("event_type_id");
            int id = Integer.parseInt(event_type_id);
            String msgId = map2.get("event_msg_id");
            if (id == 1) {
                Map<String, String> mapList = new HashMap<>();
                /** 查找活动 */
                List<Map<String, String>> list1 = userConcernUserMapper.getEventActivity(msgId, userId);
                for (Map map1 : list1
                ) {
                    mapList.putAll(map1);
                }
                lists.get(i).putAll(mapList);
            }
            /** 发布笔记 */
            else if (id == 2) {
                Map<String, String> mapList = new HashMap<>();
                /** 查找笔记 */
                List<EventUserNoteVo> eventUserNoteVoList = userConcernUserMapper.getEventUserNoteMap(msgId, userId);
                for (EventUserNoteVo eventUserNoteVo : eventUserNoteVoList
                ) {
                    Map map = ObjToMap.objectToMap(eventUserNoteVo);
                    mapList.putAll(map);
                }
                lists.get(i).putAll(mapList);
            }
            /** 分享活动 */
            else if (id == 3) {
                Map<String, String> mapList = new HashMap<>();
                List<Map<String, String>> list1 = userConcernUserMapper.getEventActivity(msgId, userId);
                for (Map map1 : list1
                ) {
                    mapList.putAll(map1);
                }
                lists.get(i).putAll(mapList);

            }
            /** 分享笔记 */
            else if (id == 4) {
                Map<String, String> mapList = new HashMap<>();
                /** 查找笔记 */
                List<EventUserNoteVo> eventUserNoteVoList = userConcernUserMapper.getEventUserNoteMap(msgId, userId);
                for (EventUserNoteVo eventUserNoteVo : eventUserNoteVoList
                ) {
                    Map map = ObjToMap.objectToMap(eventUserNoteVo);
                    mapList.putAll(map);
                }
                lists.get(i).putAll(mapList);
            }
        }

        return lists;
    }
}
